package P4;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 4.1 编写显示www.xsyu.edu.cn地址以及本地机器地址的程序
 */
public class IPAddressDisplay {
    public static void main(String[] args) {
        displayIPAddress("www.xsyu.edu.cn");
        displayLocalIPAddress();
    }

    private static void displayIPAddress(String hostname) {
        try {
            InetAddress address = InetAddress.getByName(hostname);
            System.out.println("IP address of " + hostname + ": " + address.getHostAddress());
        } catch (UnknownHostException e) {
            System.err.println("Unable to find IP address for " + hostname);
        }
    }

    private static void displayLocalIPAddress() {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println("Local machine's IP address: " + localHost.getHostAddress());
        } catch (UnknownHostException e) {
            System.err.println("Unable to determine the local machine's IP address");
        }
    }
}